<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';

class dao_category {

    //Attributes
    private $journal_id;
    private $title;
    private $thumb;
    private $print_issn;
    private $online_issn;
    private $publish;
    private $photo;
    private $creation_date;
    private $impact_factor;

    //Methods
    public function __construct() {
        
    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();
        
        $result = mysqli_query($con, "CALL GetAllCategories()")
                or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect($con);
        return $list;
    }

    public function save($category_id, $title, $descrition, $journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();
        //run the store proc
        $result = mysqli_query($con, "CALL addCategory(" . $category_id . ",'" . $title . "','" . $descrition . "'," . $journal_id . ")")
                or die("Query fail: " . mysqli_error());
        $db->close_connect($con);
        return TRUE;
    }

    public function get_by_id($id) {
        $db = new db_connection();
        $con = $db->open_connect();
        //run the store proc
        $result = mysqli_query($con, "CALL GetCategoryById(" . $id . ")")
            or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($id) {
        $db = new db_connection();
        $con = $db->open_connect();
        //run the store proc
        mysqli_query($con, "DELETE FROM tblcategories WHERE CategoryID=" . $id)
                or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return TRUE;
    }

    function get_all_categories_by_journal_id($journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();
        //run the store proc
        $result = mysqli_query($con, "CALL GetAllCategoriesByJournalID(" . $journal_id . ")")
                or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }
    
}
